package com.ss.SmartStock.controller.user;

import com.ss.SmartStock.common.*;
import com.ss.SmartStock.constant.JwtClaimsConstant;
import com.ss.SmartStock.exception.BusinessException;
import com.ss.SmartStock.pojo.dto.user.UserLoginRequest;
import com.ss.SmartStock.pojo.dto.user.UserRegisterRequest;
import com.ss.SmartStock.pojo.entity.User;
import com.ss.SmartStock.pojo.vo.LoginUserVO;
import com.ss.SmartStock.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author TsingFeng
 * @date 2024/6/17 17:42
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户管理接口")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private JwtProperties jwtProperties;


    /**
     * 登录
     * @param userLoginRequest
     * @param request
     * @return
     */

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public BaseResponse<LoginUserVO> login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        //参数校验
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        log.info("用户登录：{}", userLoginRequest);


        User user = userService.login(userLoginRequest,request);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());

        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        LoginUserVO loginUserVO = LoginUserVO.builder()
                .id(user.getId())
                .userName(user.getUserName())
                .userRole(user.getRoles())
                .createTime(user.getCreateTime())
                .updateTime(user.getUpdateTime())
                .token(token)
                .build();

        return Result.success(loginUserVO);
    }

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }

    /**
     * 用户注销
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户注销")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }




}
